/*************************************************************************
    > File Name: propagation_opt.c
    > Author: wusihai
    > Mail: wusihai18@gmail.com 
    > Created Time: Fri 14 Feb 2020 10:30:32 AM CST
 ************************************************************************/

#include "rtmlib.h"
#include "slavertm.h"
#include <athread.h>
#define PI 3.14159265354

extern SLAVE_FUN(Supdate_Vel)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_T)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_Vel_back)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_T_back)(rtm_slave_parameter* p);
extern SLAVE_FUN(Smerge)(rtm_slave_merge* p);

extern SLAVE_FUN(Supdate_VelX)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_VelY)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_VelZ)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_TX)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_TY)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_TZ)(rtm_slave_parameter* p);

extern SLAVE_FUN(Supdate_Vel_backX)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_Vel_backY)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_Vel_backZ)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_T_backX)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_T_backY)(rtm_slave_parameter* p);
extern SLAVE_FUN(Supdate_T_backZ)(rtm_slave_parameter* p);


#define PACK_SLAVE
//合并应力场
void mergeStress(){
  //索引变量
  int i,j,k;
  //确定新的计算区域位置
  int stX=0,endX=SubNx;
  int stY=0,endY=SubNy;
  int stZ=0,endZ=SubNz;
#ifdef PACK_SLAVE
  parameter_merge.stX=stX;
  parameter_merge.endX=endX;
  parameter_merge.stY=stY;
  parameter_merge.endY=endY;
  parameter_merge.stZ=stZ;
  parameter_merge.endZ=endZ;
  parameter_merge.Pin1=Txx;
  parameter_merge.Pin2=Tyy;
  parameter_merge.Pin3=Tzz;
  parameter_merge.Pout=T;
  athread_spawn(Smerge,&parameter_merge);
  athread_join();
#else
  //合并应力分量
  for(i=0;i<SubNx;i++){
	for ( j = 0; j < SubNy; j++){
	  for ( k = 0; k < SubNz; k++){
		T[n3d_ext(i,j,k)]=Txx[n3d_ext(i,j,k)] +Tyy[n3d_ext(i,j,k)]+Tzz[n3d_ext(i,j,k)];
	  }
	}
  }
#endif
}
void mergeStress_extrap(){
  //索引变量
  int i,j,k;
  //确定新的计算区域位置
  int stX=0,endX=SubNx;
  int stY=0,endY=SubNy;
  int stZ=0,endZ=SubNz;
#ifdef PACK_SLAVE
  parameter_merge.Pin1=extrapTxx;
  parameter_merge.Pin2=extrapTyy;
  parameter_merge.Pin3=extrapTzz;
  parameter_merge.Pout=extrapT;
  parameter_merge.stX=stX;
  parameter_merge.endX=endX;
  parameter_merge.stY=stY;
  parameter_merge.endY=endY;
  parameter_merge.stZ=stZ;
  parameter_merge.endZ=endZ;
  athread_spawn(Smerge,&parameter_merge);
  athread_join();
#else
  //合并应力分量
  for(i=0;i<SubNx;i++){
	for ( j = 0; j < SubNy; j++){
	  for ( k = 0; k < SubNz; k++){
		extrapT[n3d_ext(i,j,k)]=extrapTxx[n3d_ext(i,j,k)] +extrapTyy[n3d_ext(i,j,k)]+extrapTzz[n3d_ext(i,j,k)];
	  }
	}
  }
#endif
}
//1.正传
//更新速度场
void update_Vel1(){
  //X方向
  parameter.stX  =  stX;
  parameter.endX =  stX+5;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_VelX,&parameter);
  athread_join();
  parameter.stX  =  endX-5;
  parameter.endX =  endX;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_VelX,&parameter);
  athread_join();

  //Y方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY;
  parameter.endY =  stY+5;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_VelY,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  endY-5;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_VelY,&parameter);
  athread_join();
  //Z方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  stZ;
  parameter.endZ =  stZ+5;
  athread_spawn(Supdate_VelZ,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  endZ-5;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_VelZ,&parameter);
  athread_join();
}
//更新速度场
void update_Vel2(){
  parameter.stX=stX+5;
  parameter.endX=endX-5;
  parameter.stY=stY+5;
  parameter.endY=endY-5;
  parameter.stZ=stZ+5;
  parameter.endZ=endZ-5;
  //启动从核
  athread_spawn(Supdate_Vel,&parameter);
}

//更新应力场
void update_T1(){
  //X方向
  parameter.stX  =  stX;
  parameter.endX =  stX+5;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_TX,&parameter);
  athread_join();
  parameter.stX  =  endX-5;
  parameter.endX =  endX;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_TX,&parameter);
  athread_join();
  //Y方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY;
  parameter.endY =  stY+5;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_TY,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  endY-5;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_TY,&parameter);
  athread_join();
  //Z方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  stZ;
  parameter.endZ =  stZ+5;
  athread_spawn(Supdate_TZ,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  endZ-5;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_TZ,&parameter);
  athread_join();
}
//更新应力场
void update_T2(){
  parameter.stX=stX+5;
  parameter.endX=endX-5;
  parameter.stY=stY+5;
  parameter.endY=endY-5;
  parameter.stZ=stZ+5;
  parameter.endZ=endZ-5;

  //启动从核
  athread_spawn(Supdate_T,&parameter);
}


//2.重建
void update_Vel1_back(){
  //X方向
  parameter.stX  =  stX;
  parameter.endX =  stX+5;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_Vel_backX,&parameter);
  athread_join();
  parameter.stX  =  endX-5;
  parameter.endX =  endX;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_Vel_backX,&parameter);
  athread_join();

  //Y方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY;
  parameter.endY =  stY+5;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_Vel_backY,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  endY-5;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_Vel_backY,&parameter);
  athread_join();
  //Z方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  stZ;
  parameter.endZ =  stZ+5;
  athread_spawn(Supdate_Vel_backZ,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  endZ-5;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_Vel_backZ,&parameter);
  athread_join();
}
//更新速度场
void update_Vel2_back(){
  parameter.stX=stX+5;
  parameter.endX=endX-5;
  parameter.stY=stY+5;
  parameter.endY=endY-5;
  parameter.stZ=stZ+5;
  parameter.endZ=endZ-5;
  //启动从核
  athread_spawn(Supdate_Vel_back,&parameter);
}

//更新应力场
void update_T1_back(){
  //X方向
  parameter.stX  =  stX;
  parameter.endX =  stX+5;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_T_backX,&parameter);
  athread_join();
  parameter.stX  =  endX-5;
  parameter.endX =  endX;
  parameter.stY  =  stY;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_T_backX,&parameter);
  athread_join();
  //Y方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY;
  parameter.endY =  stY+5;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_T_backY,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  endY-5;
  parameter.endY =  endY;
  parameter.stZ  =  stZ;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_T_backY,&parameter);
  athread_join();
  //Z方向
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  stZ;
  parameter.endZ =  stZ+5;
  athread_spawn(Supdate_T_backZ,&parameter);
  athread_join();
  parameter.stX  =  stX+5;
  parameter.endX =  endX-5;
  parameter.stY  =  stY+5;
  parameter.endY =  endY-5;
  parameter.stZ  =  endZ-5;
  parameter.endZ =  endZ;
  athread_spawn(Supdate_T_backZ,&parameter);
  athread_join();
}
//更新应力场
void update_T2_back(){
  parameter.stX=stX+5;
  parameter.endX=endX-5;
  parameter.stY=stY+5;
  parameter.endY=endY-5;
  parameter.stZ=stZ+5;
  parameter.endZ=endZ-5;

  //启动从核
  athread_spawn(Supdate_T_back,&parameter);
}

//3.外推
void update_Vel1_extrap(){
  //X方向
  parameter_extrap.stX  =  stX;
  parameter_extrap.endX =  stX+5;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_Vel_backX,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  endX-5;
  parameter_extrap.endX =  endX;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_Vel_backX,&parameter_extrap);
  athread_join();

  //Y方向
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  stY+5;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_Vel_backY,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  endY-5;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_Vel_backY,&parameter_extrap);
  athread_join();
  //Z方向
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY+5;
  parameter_extrap.endY =  endY-5;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  stZ+5;
  athread_spawn(Supdate_Vel_backZ,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY+5;
  parameter_extrap.endY =  endY-5;
  parameter_extrap.stZ  =  endZ-5;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_Vel_backZ,&parameter_extrap);
  athread_join();
}
//更新速度场
void update_Vel2_extrap(){
  parameter_extrap.stX=stX+5;
  parameter_extrap.endX=endX-5;
  parameter_extrap.stY=stY+5;
  parameter_extrap.endY=endY-5;
  parameter_extrap.stZ=stZ+5;
  parameter_extrap.endZ=endZ-5;
  //启动从核
  athread_spawn(Supdate_Vel_back,&parameter_extrap);
}

//更新应力场
void update_T1_extrap(){
  //X方向
  parameter_extrap.stX  =  stX;
  parameter_extrap.endX =  stX+5;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_T_backX,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  endX-5;
  parameter_extrap.endX =  endX;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_T_backX,&parameter_extrap);
  athread_join();
  //Y方向
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY;
  parameter_extrap.endY =  stY+5;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_T_backY,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  endY-5;
  parameter_extrap.endY =  endY;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_T_backY,&parameter_extrap);
  athread_join();
  //Z方向
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY+5;
  parameter_extrap.endY =  endY-5;
  parameter_extrap.stZ  =  stZ;
  parameter_extrap.endZ =  stZ+5;
  athread_spawn(Supdate_T_backZ,&parameter_extrap);
  athread_join();
  parameter_extrap.stX  =  stX+5;
  parameter_extrap.endX =  endX-5;
  parameter_extrap.stY  =  stY+5;
  parameter_extrap.endY =  endY-5;
  parameter_extrap.stZ  =  endZ-5;
  parameter_extrap.endZ =  endZ;
  athread_spawn(Supdate_T_backZ,&parameter_extrap);
  athread_join();
}
//更新应力场
void update_T2_extrap(){
  parameter_extrap.stX=stX+5;
  parameter_extrap.endX=endX-5;
  parameter_extrap.stY=stY+5;
  parameter_extrap.endY=endY-5;
  parameter_extrap.stZ=stZ+5;
  parameter_extrap.endZ=endZ-5;

  //启动从核
  athread_spawn(Supdate_T_back,&parameter_extrap);
}


